package com.longma.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.longma.server.dao.MonthDataDAO;
import com.longma.server.entity.MonthData;
import com.longma.server.entity.YearData;
import com.longma.server.entity.Yuncang;
import com.longma.server.pojo.vo.YuncangYearStatisticDataVO;
import com.longma.server.pojo.vo.home.PointVO;
import com.longma.server.service.IYearDataService;
import com.longma.server.service.IYuncangUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 年发电量、年电量数据  服务类
 */
@Service
@Transactional
public class YearDataServiceImpl implements IYearDataService {
    @Autowired
    private MonthDataDAO monthDataDAO;

    @Autowired
    private IYuncangUserService yuncangUserService;

    /*
     * 根据云仓id获取当年用电量、发电量
     */
    @Override
    public YearData getYearData(Integer yuncangId) {
        int year = LocalDateTime.now().getYear();

        // 构建返回实体
        YearData yearData = new YearData();
        yearData.setYuncangId(yuncangId);
        yearData.setGeneration(0.0);
        yearData.setGeneration(0.0);
        yearData.setYear(year);


        List<MonthData> monthDatas = monthDataDAO.list(Wrappers.<MonthData>lambdaQuery().eq(MonthData::getYuncangId,yuncangId)
        .eq(MonthData::getYear,year));

        double consumption = 0.0;
        double generation = 0.0;
        for (MonthData monthData:monthDatas) {
            consumption += monthData.getConsumption();
            generation += monthData.getGeneration();
        }

        yearData.setConsumption(consumption);
        yearData.setGeneration(generation);
        return yearData;
    }

    /**
     * 获取登录用户所拥有云仓当年用电合计曲线
     */
    @Override
    public List<PointVO> getYearConsumptionCurve() {
        return getYearCurve(false);
    }

    /**
     * 获取登录用户所拥有云仓年当发电合计曲线
     */
    @Override
    public List<PointVO> getYearGenerationCurve() {
        return getYearCurve(true);
    }

    /**
     * 获取云仓年统计数据  每年发电量、用电量
     */
    @Override
    public Map<Integer,Map<Integer, YuncangYearStatisticDataVO>> getYearStatisticData(List<Integer> yuncangIds) {
        Map<Integer,Map<Integer, YuncangYearStatisticDataVO>> result = new HashMap<>();

        if(CollUtil.isEmpty(yuncangIds)){
            return result;
        }

        // 查询云仓月数据
        List<MonthData> monthDatas = monthDataDAO.list(Wrappers.<MonthData>lambdaQuery().in(MonthData::getYuncangId,yuncangIds));
        for(MonthData monthData : monthDatas){
            Integer yuncangId = monthData.getYuncangId();
            Integer year = monthData.getYear();
            Map<Integer, YuncangYearStatisticDataVO> map = result.get(yuncangId);
            if(map == null ){
                map = new HashMap<>();
            }

            // 獲取年數據
            YuncangYearStatisticDataVO data = map.get(year);
            if(data == null){
                data = new YuncangYearStatisticDataVO(0.0,0.0);
            }
            data.setConsumption(data.getConsumption() + monthData.getConsumption());
            data.setGeneration(data.getGeneration() + monthData.getGeneration());
            map.put(year,data);
            result.put(yuncangId,map);
        }

        return result;
    }

    /**
     * 年发电or用电曲线
     * @param generation: true 发电  false用电
     */
    private List<PointVO> getYearCurve(boolean generation) {
        // 初始化数据
        List<PointVO> res = new ArrayList<>();

        // 获取当前年份
        int year = LocalDateTime.now().getYear();

        // 按顺序存放每个月发电or用电量
        for(int i=1;i<=12;i++){
            res.add(new PointVO(String.format("%04d-%02d",year,i),0.0));
        }

        // 获取云仓id
        List<Integer> yuncangIds = yuncangUserService.getYuncangs().stream().map(Yuncang::getId).collect(Collectors.toList());

        // 不存在云仓
        if (CollUtil.isEmpty(yuncangIds)){
            return res;
        }

        // 查询云仓月数据
        List<MonthData> monthDatas = monthDataDAO.list(Wrappers.<MonthData>lambdaQuery().eq(MonthData::getYear,year)
                .in(MonthData::getYuncangId,yuncangIds));

        for(MonthData monthData:monthDatas){
            PointVO pointVO = res.get(monthData.getMonth() - 1);
            if(generation) {
                pointVO.setValue(pointVO.getValue() + monthData.getGeneration());
            }else{
                pointVO.setValue(pointVO.getValue() + monthData.getConsumption());
            }
        }
        return res;
    }
}
